/**
 * Created by rongyao on 2017/10/19.
 */
var mongoose = require('mongoose');
const Comment = require('../models/comment.model');

exports.create = function (req, res, next) {
	var comment = new Comment(req.body);
	comment.save().then(data => {
		res.json(data);
	});
}

exports.get = function (req, res, next) {
	var id = req.params.id;
	Comment.findById(id, function (err, data) {
		res.json(data);
	})
}

exports.update = function (req, res, next) {
	var id = req.params.id;
	Comment.findByIdAndUpdate(id, {$set: req.body}, {new: false})
		.then(data => {
			res.json(data);
		})
}

exports.remove = function (req, res, next) {
	var id = req.params.id;
	Comment.findByIdAndRemove(id, function (data) {
		res.json(data);
	})
}

exports.list = function (req, res, next) {
	var page = req.body.page ? req.body.page : 1;
	var limit = req.body.rows ? req.body.rows : 10;
	var queryCondition = {};
	if (req.body.keywords && req.body.keywords.trim().length > 0) {
		name = req.body.keywords;
		queryCondition = {
			"commentator": new RegExp(name, 'i'),
			// newsId:req.body.newsId
		}
	}
	
	if (req.body.cateId && req.body.cateId.length > 0) {
		// console.log(111);
		queryCondition.cateId = req.body.cateId;
	}
	console.log(queryCondition);
	Comment.paginate(queryCondition, {page: page - 0, limit: limit - 0, sort: {date: -1}}, function (err, result) {
		// console.log(result);
		result.rows = result.docs;
		delete result.docs;
		res.json(result);
	});
}

exports.count = function (req, res, next) {
	// console.log(req.body.newsId);
	Comment.find({newsId:req.body.newsId},function (err,data) {
		res.json(5)
	})
	
}

exports.deletes = function (req, res, next) {
	var ids = req.body.ids;
	console.log(ids);
	console.log(ids.split(','));
	if(ids.split(',').length>0){
		Comment.remove({_id: {$in: ids.split(',')}}).then(data=>{
			res.json({'msg':"删除成功"})
		})
	}
}